<?php
namespace jianan\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use jianan\models\Cus;
use jianan\models\Master;
use yii\db\Query;
header("Content-type:text/html;charset=utf-8");
/*
* 销售情况统计
*/

class SaleController extends Controller {

	/**
	 * 总数据
	 * @return [type] [description]
	 */
	public function actionStatistics(){
		$model = new Cus;

		$query = Cus::find();		
		$countQuery = clone $query;
		$pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>10]);

		$model = $query->offset($pages->offset)->limit($pages->limit)->orderBy(['id'=> SORT_DESC])->asArray()->all();

		foreach($model as $k => $v){
			$mastername = Master::findOne($v['sid']);
			$v['sid'] = isset($mastername['masterName']) ? $mastername['masterName'] : "无";
		}
		$gender = [1=>'男',2=>'女'];
		$data = Cus::$data;

		return $this->render('statistics',['model'=>$model,'pages'=>$pages,'gender'=>$gender,'data'=>$data]);
	}

	/**
	 * 今日数据
	 * @return [type] [description]
	 */
	public function actionToday(){
		$model = new Cus;

		$query = Cus::find();		
		$countQuery = clone $query;
		$pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>10]);

		$today = date('Y-m-d',time());
		$model = $query->where(["from_unixtime(visitTime,'%Y-%m-%d')"=>$today])->offset($pages->offset)->limit($pages->limit)->orderBy(['id'=> SORT_DESC])->all();

		foreach($model as $k => $v){
			$mastername = Master::findOne($v['sid']);
			$v['sid'] = isset($mastername['masterName']) ? $mastername['masterName'] : "无";
		}
		$gender = [1=>'男',2=>'女'];
		$data = Cus::$data;

		return $this->render('today',['model'=>$model,'pages'=>$pages,'gender'=>$gender,'data'=>$data]);
	}

	/**
	 * 本周数据
	 * @return [type] [description]
	 */
	public function actionWeek(){
		$model = new Cus;

		$query = Cus::find();		
		$countQuery = clone $query;
		$pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>10]);

		$date = strtotime('-7 days');
		//var_dump($date);exit;
		$model = $query->where(['>=','visitTime',$date])->orderBy(['id'=> SORT_DESC])->all();

		foreach($model as $k => $v){
			$mastername = Master::findOne($v['sid']);
			$v['sid'] = isset($mastername['masterName']) ? $mastername['masterName'] : "无";
		}
		$gender = [1=>'男',2=>'女'];
		$data = Cus::$data;

		return $this->render('week',['model'=>$model,/*'pages'=>$pages,*/'gender'=>$gender,'data'=>$data]);
	}

	/**
	 * 上半月数据
	 * @return [type] [description]
	 */
	public function actionPre(){
		$model = new Cus;

		$query = Cus::find();		
		$countQuery = clone $query;
		$pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>10]);

		$model = $this->_getPre();

		foreach($model as $k => $v){
			$mastername = Master::findOne($v['sid']);
			$v['sid'] = isset($mastername['masterName']) ? $mastername['masterName'] : "无";
		}
		$gender = [1=>'男',2=>'女'];
		$data = Cus::$data;

		return $this->render('pre',['model'=>$model,/*'pages'=>$pages,*/'gender'=>$gender,'data'=>$data]);
	}

	/**
	 * 下半月数据
	 * @return [type] [description]
	 */
	public function actionLast(){
		$model = new Cus;

		$query = Cus::find();		
		$countQuery = clone $query;
		$pages = new Pagination(['totalCount'=>$countQuery->count(),'pageSize'=>10]);

		$today = date('Y-m-d',time());
		$model = $this->_getLast();

		foreach($model as $k => $v){
			$mastername = Master::findOne($v['sid']);
			$v['sid'] = isset($mastername['masterName']) ? $mastername['masterName'] : "无";
		}
		$gender = [1=>'男',2=>'女'];
		$data = Cus::$data;

		return $this->render('last',['model'=>$model,/*'pages'=>$pages,*/'gender'=>$gender,'data'=>$data]);
	}

	/**
	 * 上半月
	 */
	private static function _getPre(){
		$model = new Query;
		$today = date('Y-m',time());
		$month = date('m',time());
		$middle = mktime(23,59,59,$month,15);
		$re = $model->select('*')->from('jianan_cus')->where(["from_unixtime(visitTime,'%Y-%m')" => $today])->andWhere(['<=','visitTime',$middle])->orderBy(["from_unixtime(visitTime,'%Y-%m-%d')" => SORT_ASC])->all();
		
		return $re;
	}

	/**
	 * 下半月
	 */
	private static function _getLast(){
		$model = new Query;
		$today = date('Y-m',time());
		$month = date('m',time());
		$middle = mktime(23,59,59,$month,15);
		$re = $model->select('*')->from('jianan_cus')->where(["from_unixtime(visitTime,'%Y-%m')" => $today])->andWhere(['>','visitTime',$middle])->orderBy(["from_unixtime(visitTime,'%Y-%m-%d')" => SORT_DESC])->all();

		return $re;
	}
}
